/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package spider20;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Mikael Köhlmyr | mikael@kohlmyr.com | http://www.kmyr.net
 */
public class Spider {
    public ArrayList<String> people = new ArrayList<String>();
    public ArrayList<Integer> points = new ArrayList<Integer>();
    private int counter = 0;
    private static final int limit = 190; // i've found generally that this is where you stop finding new results

    public void weave(String input){
        people.add(input);
        points.add(0);
        runner(input);
        /**
        sort();
        
        for(int i = 0; i < 5; i++){
            System.out.println(points.get(i) + "pts > " + people.get(i));
        }
        */
    }

    public void runner(String current){ // change to String current and just call self instead of loop?
        ArrayList<String> result;
        String content;
        System.out.println(current + " was scanned");
        content = getContentFromUrl("http://www.ecs.soton.ac.uk/people/" + current);
        result = getPeopleFromString(content, current);
        for(String r : result){
            if(people.contains(r)){
                int index = people.indexOf(r);
                points.set(index, points.get(index) + 1);
                //System.out.println(r + " is at " + points.get(index) + " points");
            }
            else{
                //System.out.println("Found first " + r);
                people.add(r);
                points.add(1);
            }
        }
        if(counter < people.size() && counter < limit){
            counter++;
            runner(people.get(counter));
        }
    }

    public void sort(){
        int n = points.size();
        // Bubble sort, terrible but I cba.
        // High values will move up
        for(int k = 1; k < n; k++){
            for(int i = 0; i < n-k; i++){//Moves from 0 to n-k-1 which becomes smaller for each outer pass(k)
                if(points.get(i) < points.get(i + 1)){// i+1 works because of the implied -1 of the < in the surrounding loop
                    int tempPoints = points.get(i);
                    String tempPerson = people.get(i);
                    points.set(i, points.get(i + 1));
                    points.set(i + 1, tempPoints);
                    people.set(i, people.get(i + 1));
                    people.set(i + 1, tempPerson);
                }
            }
        }
    }

    private ArrayList getPeopleFromString(String input, String disregard){
        ArrayList<String> found = new ArrayList<String>();
        Pattern pat = Pattern.compile("people/[a-zA-Z0-9]+");
        Matcher mat = pat.matcher(input);
        while(mat.find()){
            String ref = mat.group().substring(7);
            if(ref.equals(disregard) == false){
                found.add(ref);
            }
        }
        return found;
    }

    public static String getContentFromUrl(String link){
        String line;
        String page = "";
        URL url;
        try {
            url = new URL(link);
            BufferedReader in;
            try {
                in = new BufferedReader(new InputStreamReader(url.openStream()));
                while ((line = in.readLine()) != null) {
                    page += line;
                }
                in.close();
            } catch (IOException ex) {
                System.out.println("IOException caught in getPageFromUrl");
                return null;
            }

        } catch (MalformedURLException ex) {
            System.out.println("MalformedURLException caught in getPageFromUrl");
            return null;
        }
        
        return page;
    }
}
